package uebung06_h;

import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

public class Circle {
	double r, cx, cy;
	
	public void setCircle(Ellipse2D el) {
		this.cx = el.getCenterX();
		this.cy = el.getCenterY();
		this.r = (el.getMaxY()-el.getMinY())/2.;
	}
	
	public double getDistance(double x, double y) {
		double dx = x-this.cx, dy = y-this.cy, distance = Math.abs(Math.sqrt(dx*dx+dy*dy));
		if (distance<r) {
			return r-distance;
		}
		else {
			return distance-r;
		}
	}
	
	public Point2D getNearest(double x, double y) {
		Point2D result = new Point2D.Double();
		double dx = x-this.cx, dy = y-this.cy, distance = Math.abs(Math.sqrt(dx*dx+dy*dy)), nx = dx/distance, ny = dy/distance;
		result.setLocation((nx*r-dx)+x, (ny*r-dy)+y);
		
		return result;
	}
}
